/*
 *	Yalala
 *	
 *	Copyright © MediaMonks B.V.
 *	All rights reserved.
 *	
 *	Redistribution and use in source and binary forms, with or without
 *	modification, are permitted provided that the following conditions are met:
 *	1. Redistributions of source code must retain the above copyright
 *	   notice, this list of conditions and the following disclaimer.
 *	2. Redistributions in binary form must reproduce the above copyright
 *	   notice, this list of conditions and the following disclaimer in the
 *	   documentation and/or other materials provided with the distribution.
 *	3. All advertising materials mentioning features or use of this software
 *	   must display the following acknowledgement:
 *	   This product includes software developed by MediaMonks B.V.
 *	4. Neither the name of MediaMonks B.V. nor the
 *	   names of its contributors may be used to endorse or promote products
 *	   derived from this software without specific prior written permission.
 *	
 *	THIS SOFTWARE IS PROVIDED BY MEDIAMONKS B.V. ''AS IS'' AND ANY
 *	EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *	WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *	DISCLAIMED. IN NO EVENT SHALL MEDIAMONKS B.V. BE LIABLE FOR ANY
 *	DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *	ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *	
 */

package com.mediamonks.yalala.ui.table 
{
	import nl.acidcats.yalog.common.MessageData;

	import temple.ui.behaviors.textfield.TextFieldColorizer;
	import temple.utils.TimeUtils;

	import com.mediamonks.yalala.ui.LevelColors;

	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	/**
	 * @author Thijs Broerse
	 */
	public class Row extends Sprite
	{
		/**
		 * The maximum length (in characters) of the message. If the message is longer, it will be truncated.
		 */
		private static const _MAX_MESSAGE_LENGTH:uint = 200;
		
		private var _timeField:TextField;
		private var _messageField:TextField;
		private var _messageFieldColor:TextFieldColorizer;
		private var _data:MessageData;
		
		public function Row() 
		{
			this.init();
		}

		private function init():void 
		{
			var tf:TextFormat = new TextFormat();
			tf.bold = true;
			tf.font = "_sans";
			tf.size = 10;
			tf.color = 0xDDDDDD;

			this._timeField = new TextField();
			this._timeField.width = 60;
			this._timeField.height = 18;
			this._timeField.defaultTextFormat = tf;
			this.addChild(this._timeField);

			this._messageField = new TextField();
			this._messageField.x = this._timeField.width;
			this._messageField.height = 18;
			this._messageField.defaultTextFormat = tf;
			this._messageField.multiline = false;
			this._messageField.autoSize = TextFieldAutoSize.LEFT;
			this.addChild(this._messageField);
			
			this._messageFieldColor = new TextFieldColorizer(this._messageField, 0xffffff, 0xffff00, 0x0000ff);
		}
		
		public function get data():MessageData
		{
			return this._data;
		}

		public function set data(value:MessageData):void 
		{
			if (this._data == value) return;
			
			this._data = value;
			
			if (value)
			{
				this.visible = true;
				
				this._messageFieldColor.textColor = LevelColors.COLORS[value.level];
		
				// time
				if (value.time == 0)
				{
					this._timeField.text = "";
				}
				else if (!isNaN(value.time)) 
				{
					this._timeField.text = TimeUtils.formatTime(value.time);
				}
				else
				{
					this._timeField.text = "--";
				}
				
				// message
				var s:String;
				
				if (value.text && value.text.length > _MAX_MESSAGE_LENGTH)
				{
					s = value.text.substr(0, _MAX_MESSAGE_LENGTH) + "...";
				}
				else if (value.text)
				{
					s = value.text;
				}
				else
				{
					s = "";
				}
				
				if (value.sender != null) s += "\t\t-- " + value.sender;
				
				if (value.senderId) s += " | id:" + value.senderId;
				if (value.stackTrace) s += " | " + value.stackTrace;
				
				this._messageField.text = s;
				
				// reset selection
				if (this._messageField.selectionBeginIndex || this._messageField.selectionEndIndex)
				{
					this._messageField.setSelection(0, 0);
				}
			}
			else
			{
				this.visible = false;
			}
		}

		public function highLight(text:String):void 
		{
			this.graphics.clear();
			
			if (!text) return;
			
			this.graphics.beginFill(0xffff00, .1);
			this.graphics.lineStyle(1, 0xffff00, .8);
			
			var offset:uint;
			var index:int = this._messageField.text.indexOf(text, offset);
			while (index != -1)
			{
				var left:Number = this._messageField.getCharBoundaries(index).left + this._messageField.x;
				var right:Number = this._messageField.getCharBoundaries(index + text.length - 1).right + this._messageField.x;
				this.graphics.drawRect(left, 2, right - left, 12);
				
				offset = index + text.length;
				index = this._messageField.text.indexOf(text, offset);
			}
			this.graphics.endFill();
		}
	}
}
